﻿using Furion;
using Microsoft.Extensions.Caching.Memory;
using OAS.Application.UserApp.Dtos;
using OAS.Application;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Claims;
using OAS.Core;

namespace OAS.Application
{
    public class UserContext
    {
        public static string UserId
        {
            get
            {
                return App.User?.FindFirstValue("UserId");
            }
        }
        public static string UserName
        {
            get
            {
                return App.User?.FindFirstValue("UserName");
            }
        }
        private static IUserService UserService
        {
            get
            {
                return App.GetService<IUserService>();
            }
        }
        public static DtoUserInfo CurrentUser
        {
            get
            {
                return UserService.GetUserInfo(UserName);
            }
        }
        public static bool IsAdmin
        {
            get
            {
                return CurrentUser.RoleName == "超级管理员";
            }
        }

        /// <summary>
        /// 处理获取对象异常问题
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="action">获取对象委托</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>T</returns>
        private static T CatchOrDefault<T>(Func<T> action, T defaultValue = null)
            where T : class
        {
            try
            {
                return action();
            }
            catch
            {
                return defaultValue ?? null;
            }
        }
    }
}
